package com.bdth.bdthiss.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bdth.bdthiss.entity.Customer;
import com.bdth.bdthiss.mapper.CustomerMapper;
import com.bdth.bdthiss.service.CustomerService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bdth.bdthiss.vo.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 客户信息 服务实现类
 * </p>
 *
 * @author LGP
 * @since 2019-08-22
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public Page<Customer> listPages(Pagination pagination, Map<String, Object> query) {
        EntityWrapper<Customer> entityWrapper = new EntityWrapper<>();
        String compname = (String)query.get("compname");
        if(compname!=null && !compname.trim().equals(""))
            entityWrapper.like("compname",compname);
        String dutynumber = (String)query.get("dutynumber");
        if(dutynumber!=null && !dutynumber.trim().equals(""))
            entityWrapper.like("dutynumber",dutynumber);
        String headname = (String)query.get("headname");
        if(headname!=null && !headname.trim().equals(""))
            entityWrapper.like("headname",headname);
        if(pagination.getOrder()!=null) {
            if (pagination.getOrder().toLowerCase().equals("asc")){
                entityWrapper.orderBy(pagination.getSort(), true);
            }else{
                entityWrapper.orderBy(pagination.getSort(), false);
            }
        }
        try {
            Integer signmanid = (Integer) query.get("signmanid");
            if (signmanid != null)
                entityWrapper.in("signmanid", signmanid+"");
        }catch (Exception ex){
            String signmanid = (String) query.get("signmanid");
            if (signmanid != null)
                entityWrapper.in("signmanid", signmanid);
        }
        String b = (String)query.get("branch");
        if(b!=null) {
            Integer branch = Integer.parseInt(b);
            entityWrapper.eq("branch", branch);
        }
        if(pagination.getOrder()!=null) {
            if (pagination.getOrder().toLowerCase().equals("asc")){
                entityWrapper.orderBy(pagination.getSort(), true);
            }else{
                entityWrapper.orderBy(pagination.getSort(), false);
            }
        }
        Page<Customer> page = new Page<>(pagination.getPage()==null?1:pagination.getPage(),
                pagination.getRows()==null?20:pagination.getRows());

        List<Customer> items = customerMapper.selectPage(page,entityWrapper);
        page.setRecords(items);
        return page;
    }

    @Override
    public boolean update(Customer bean) {
        if(bean.getId()>0) {
            Integer result = customerMapper.updateById(bean);
            if(result>0)
                return true;
        }
        return false;
    }

    @Override
    public boolean save(Customer bean) {
        Integer result = customerMapper.insert(bean);
        if(result>0)
            return true;
        return false;
    }

    @Override
    public boolean delete(Integer id) {
        Integer result = customerMapper.deleteById(id);
        if(result>0)
            return true;
        return false;
    }

    @Override
    public boolean remove(String ids) {
        EntityWrapper<Customer> entityWrapper = new EntityWrapper<>();
        entityWrapper.in("id",ids);
        int result = customerMapper.delete(entityWrapper);
        if(result>0)
            return true;
        return false;
    }
}
